/*
 * This example demostrates how to create a object as a member variable.
 * 
 * The key point is the funciton: A &getA() //need to use reuturn "reference"
 *
 *
 *
 */

#include <iostream>

using namespace std;

class A
{
  public:
    A(): A_int(0)
    {
      cout << "A constructor is called." << endl;
    }

    int get_A_int()
    {
      cout << "get_A_int()" << endl;
      return this->A_int;
    }

    void set_A_int(int integar)
    {
      cout << "set_A_int(" << integar << ")" << endl;
      //this->A_int = integar;
      A_int = integar;
    }

  private:
    int A_int;
};

class B
{
  public:
    B(): a()
    {
      cout << "B constructor is called." << endl;
    }
 
    //A getA()  //this will not return the original copy of a.
    A &getA() //need to use reuturn "reference"
    {
      cout << "getA()" << endl;
      return this->a;
      //return this->&a;//incorrect!!!
    }

    void print_A_int()
    {
      cout << "b.print_A_int() = " << this->a.get_A_int() << endl;
    }

  private:
    A a;
};

int main()
{
  B b;

  b.getA().set_A_int(5);

  cout << "b.getA().get_A_int() = " << b.getA().get_A_int() << endl;

  b.print_A_int();

  return 0;
}
